package com.study.handler;

import com.study.common.Result;
import com.study.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理
 *
 * @Author wfw
 * @Date 2020/07/11 15:19
 */
@ControllerAdvice
@Slf4j
public class BaseExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result error(HttpServletRequest request, HttpServletResponse response, Exception e) {
        log.error("异常：",e);
        if (e.getClass() == CommonException.class) {
            CommonException commonException = (CommonException) e;
            return new Result(commonException.getCode());
        }else {
            return Result.ERROR();
        }
    }
}
